package com.example.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.auth.model.UserModelExt;
import com.example.auth.service.UserModelExtService;
import com.example.db.DBSql;
import com.example.db.SqlBuilder;
import com.example.db.annotations.Table;
import com.example.db.model.IPage;
import com.example.utils.StringUtils;

import java.util.List;

/**
 * @Classname UserModelExtServiceImpl
 * @Description TODO
 * @Date 2022/8/1 17:18
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class UserModelExtServiceImpl implements UserModelExtService {

    @Autowired
    DBSql dbSql;

    private static final String USER_EXT_TABLE=" select b.username,b.realname,a.userid,c.rolename,b.phone,c.roleno,e.title,e.href from sys_role_user a left join sys_user b ON a.userid=b.id LEFT JOIN sys_role c ON a.roleId=c.id\n" +
            "LEFT JOIN sys_role_menu d ON c.id=d.roId LEFT JOIN sys_menu e ON d.menuId=e.id ";

    @Override
    public IPage<UserModelExt> page(int pageNum, int pageSize, JSONObject obj) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.select("*").from("("+ USER_EXT_TABLE+") a");
        if(obj != null){
            Object uid=obj.get("uid");
            if(!StringUtils.isEmpty(uid)){
                sqlBuilder.and("userid").eq(uid);
            }
            Object username=obj.get("username");
            if(!StringUtils.isEmpty(username)){
                sqlBuilder.and("username").like(username);
            }
            Object phone=obj.get("phone");
            if(!StringUtils.isEmpty(phone)){
                sqlBuilder.and("phone").like(phone);
            }
            Object realname=obj.get("realname");
            if (!StringUtils.isEmpty(realname)) {
                sqlBuilder.and("realname").like(realname);
            }
            Object rolename=obj.get("rolename");
            if(!StringUtils.isEmpty(rolename)){
                sqlBuilder.and("rolename").like(rolename);
            }
            Object title=obj.get("title");
            if(!StringUtils.isEmpty(title)){
                sqlBuilder.and("title").like(title);
            }
            Object href=obj.get("href");
            if(!StringUtils.isEmpty(href)){
                sqlBuilder.and("href").like(href);
            }
        }
        sqlBuilder.page(pageNum,pageSize);
        String page=sqlBuilder.getPage();
        String countSql=sqlBuilder.getCountSql();
        List<UserModelExt> userModelExts=dbSql.selectList(page,UserModelExt.class);
        int count=dbSql.selectCount(countSql);
        IPage<UserModelExt> iPage=new IPage<UserModelExt>(count,userModelExts);
        return iPage;
    }


}
